package com.yunbao.blogs.controller;

import com.yunbao.blogs.pojo.Article;
import com.yunbao.blogs.repository.ArticleRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Calendar;

@Controller
public class IndexController {

    @Autowired
    private ArticleRepository articleRepository;

    @RequestMapping("/index")
    public String index(Model model,Integer pageNum){

        if (pageNum == null){
            pageNum = 1;
        }
        Pageable pageable = PageRequest.of(pageNum - 1, 2); // （当前页， 每页记录数）
        Page<Article> list = articleRepository.findAll(pageable);


        model.addAttribute("pageInfo", list);
        model.addAttribute("currentYear", Calendar.getInstance().get(Calendar.YEAR));
        return "client/index";
    }


}
